package com.example.yls.demoa;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private TextView tv_my;
    private TextView tv_about;
    private TextView tv_find;
    private TextView tv_messege;
    private TabLayout tableLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.vp);
        fragmentList = new ArrayList<Fragment>();
        inniteFragment();
        viewPager.setAdapter(new MyframgentPageAdapter(getSupportFragmentManager(), fragmentList));
        inniteTextView();
        tv_messege.setBackgroundColor(Color.RED);
        tv_about.setBackgroundColor(Color.GREEN);
        tv_find.setBackgroundColor(Color.GREEN);
        tv_my.setBackgroundColor(Color.GREEN);
       /* tableLayout=findViewById(R.id.tab_l);*/
      /*  tableLayout.setupWithViewPager(viewPager);*/

        tv_messege.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(0);
            }
        });
        tv_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(2);
            }
        });
        tv_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(3);
            }
        });
        tv_about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1);
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
        switch (position)
        {
            case 0:
                tv_messege.setBackgroundColor(Color.RED);
                tv_about.setBackgroundColor(Color.GREEN);
                tv_find.setBackgroundColor(Color.GREEN);
                tv_my.setBackgroundColor(Color.GREEN);
                break;
            case 1:
            tv_messege.setBackgroundColor(Color.GREEN);
            tv_about.setBackgroundColor(Color.RED);
            tv_find.setBackgroundColor(Color.GREEN);
            tv_my.setBackgroundColor(Color.GREEN);
                break;
            case 2:
            tv_messege.setBackgroundColor(Color.GREEN);
            tv_about.setBackgroundColor(Color.GREEN);
            tv_find.setBackgroundColor(Color.RED);
            tv_my.setBackgroundColor(Color.GREEN);
                break;
            case 3:
                tv_messege.setBackgroundColor(Color.GREEN);
                tv_about.setBackgroundColor(Color.GREEN);
                tv_find.setBackgroundColor(Color.GREEN);
                tv_my.setBackgroundColor(Color.RED);
                break;


        }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    private void inniteTextView() {
        tv_about=findViewById(R.id.tv_about);
        tv_find=findViewById(R.id.tv_find);
        tv_messege=findViewById(R.id.tc_messege);
        tv_my=findViewById(R.id.tv_my);

    }

    private void inniteFragment() {
        fragment01 fragment01=new fragment01();
        fragment02 fragment02=new fragment02();
        fragment03 fragment03=new fragment03();
        fragment04 fragment04=new fragment04();

        fragmentList.add(fragment01);
        fragmentList.add(fragment02);
        fragmentList.add(fragment03);
        fragmentList.add(fragment04);

    }
}
